package cn.itsource.basic.util;

import cn.itsource.basic.jwt.JwtUtils;
import cn.itsource.basic.jwt.LoginData;
import cn.itsource.basic.jwt.Payload;
import cn.itsource.basic.jwt.RsaUtils;
import cn.itsource.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;
//工具类 获取登录信息
public class LoginContext {
    public static Logininfo getLogininfo(HttpServletRequest request){
        try {
            //从请求头获取token
            String token = request.getHeader("token");
            //判断不为空
            if(token!=null){
                //获取公钥
                PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
                //解密
                Payload<LoginData> loginDataPayload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
                //获取登录信息
                return loginDataPayload.getLoginData().getLogininfo();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    return null;
    }
}
